<?php

/**
 * This is the model class for table "booked".
 *
 * The followings are the available columns in table 'booked':
 * @property integer $id
 * @property string $user_id
 * @property string $status
 * @property integer $ticket_id
 * @property string $quantity
 * @property string $payment_type
 * @property string $booked_date
 * @property string $additional_note

 * @property string $booked_time
 * @property string $ticket_order_number
 * @property string $seat
 * @property string $percentage
 * @property string $percentage_reason
 * @property string $lock_session
 * @property string $user_lock	
 * @property string $deliver_user_edit
 * @property string $finish_user_edit
 *
 * The followings are the available model relations:
 * @property Users $user
 * @property Ticket $ticket
 */
class Booked extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Booked the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'booked';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(

			array('ticket_id', 'required','message' => Yii::t('lang','ticket_check')),
            array('payment_type','required','message' => Yii::t('lang','payment_check')),
            array('phone','required','message' => Yii::t('lang','phone_required')),
            array('email_contact','required','message' => Yii::t('lang','email_required')),
            array('email_contact','email','message' => Yii::t('lang','email_format')),
            array('phone', 'numerical', 'integerOnly'=>true),
			array('user_id', 'length', 'max'=>11),
			// array('quantity', 'length', 'max'=>10),
			array('payment_type', 'length', 'max'=>255),
//			array('booked_date, additional_note,facbook_id,quantity,status, phone,payment_address,deliver_address', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, user_id,event_id , ticket_id, quantity, payment_type,percentage,percentage_reason, booked_date,booked_time,ticket_order_number,deliver_address ,status,additional_note,facbook_id,phone,payment_address,deliver_address,seat,lock_session,user_lock,deliver_user_edit,deliver_edit_time, finish_user_edit,finish_edit_time,payment_status, email_contact',
				'safe'
				),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'ticket' => array(self::BELONGS_TO, 'Ticket', 'ticket_id'),
			'booked_details' =>array(self::HAS_MANY,'BookedDetail','booked_id'),
			'event' => array(self::BELONGS_TO,'Event','event_id')
		);
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'ticket_id' => 'Ticket',
			'quantity' => 'Quantity',
			'payment_type' => 'Payment Type',
			'booked_date' => 'Booked Date',
			'additional_note' => Yii::t('lang','add_note'),
			'facbook_id' => 'Facebook Id',
			'phone' => Yii::t('lang','phone'),
			'payment_address' => Yii::t('lang','payment'),
			'deliver_address' => Yii::t('lang','deliver_address'),
			'booked_time' => 'Booked time',
			'ticket_order_number' => ' Ticket Order Number',
			'seat' => Yii::t('lang','seat'),
            'email_contact' => Yii::t('lang','email_contact'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('ticket_id',$this->ticket_id);
		$criteria->compare('quantity',$this->quantity,true);
		$criteria->compare('payment_type',$this->payment_type,true);
		$criteria->compare('booked_date',$this->booked_date,true);
		$criteria->compare('additional_note',$this->additional_note,true);
		$criteria->compare('facbook_id',$this->facbook_id,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


	public function getAll($status = NULL,$time = null)
	{
		date_default_timezone_set('UTC');
		$criteria=new CDbCriteria;
        $criteria->order ='t.id desc';
        if ($time == null) {
        	$time = date("Y-m-d",time());
        }
        if($status != NULL){
            $criteria->compare('t.status', $status);
            
        }
        $criteria->compare('t.booked_date',$time);
        $result =self::model()->findAll($criteria);
        if($result) return $result;
        return array();
	}
    
    /**
     * <Jason>
     * <pmhai90@gmail.com>
     * @return type
     */
	public function getNewNotify()
	{
		$criteria=new CDbCriteria;
        $criteria->compare('t.new_notification', 1);
        $result =self::model()->find($criteria);
        if($result) return $result->id;
        return array();
	}
        public function createTicket(){
            $tickets=array();
            $orderDetails =$this->booked_details;
            foreach($orderDetails as $detail){
                for($i=0;$i<(int)$detail->quantity;$i++){
                    $ticket=new DirectBook();
                    $ticket->event_id=$this->event_id;
                    $ticket->organizer_order_id=$this->user_id;
                    $ticket->ticket_id=$this->ticket_id;
                    $ticket->payment_status=1;
                    $ticket->barcode_hash=substr(microtime(),2,8).substr(time(),6);
                    $ticket->save();
                    $ticket->detail=$detail;
                    $tickets[]=$ticket;
                }
            }
            SendEmail::sendMailBarcode($this,$tickets);
        }





}